package cn.iocoder.yudao.module.vote.service.voteconfig;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.vote.controller.admin.voteconfig.vo.VoteConfigCreateReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteconfig.vo.VoteConfigExportReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteconfig.vo.VoteConfigPageReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteconfig.vo.VoteConfigUpdateReqVO;
import cn.iocoder.yudao.module.vote.convert.voteconfig.VoteConfigConvert;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteconfig.VoteConfigDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteinfo.VoteInfoDO;
import cn.iocoder.yudao.module.vote.dal.mysql.voteconfig.VoteConfigMapper;
import cn.iocoder.yudao.module.vote.dal.mysql.voteinfo.VoteInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.vote.enums.ErrorCodeConstants.*;

/**
 * 优秀最大比例配置信息 Service 实现类
 *
 * @author moving
 */
@Service
@Validated
public class VoteConfigServiceImpl implements VoteConfigService {

    @Resource
    private VoteConfigMapper voteConfigMapper;
    @Resource
    private VoteInfoMapper voteInfoMapper;

    @Override
    public Long createConfig(VoteConfigCreateReqVO createReqVO) {



        VoteInfoDO voteInfoDO = existVote(createReqVO.getVoteId());
        createReqVO.setVoteName(voteInfoDO.getVoteName());
        createReqVO.setVoteType(voteInfoDO.getVoteType());

        VoteConfigDO configDO = voteConfigMapper.select(createReqVO.getVoteId().intValue(),createReqVO.getDeptId(),null);
        if(configDO!=null){
            exception(CONFIG_VOTE_DEPT_EXISTS);
        }

        // 插入
        VoteConfigDO config = VoteConfigConvert.INSTANCE.convert(createReqVO);
        voteConfigMapper.insert(config);
        // 返回
        return config.getId();
    }

    @Override
    public void updateConfig(VoteConfigUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateConfigExists(updateReqVO.getId());
        VoteInfoDO voteInfoDO = existVote(updateReqVO.getVoteId());
        updateReqVO.setVoteName(voteInfoDO.getVoteName());
        updateReqVO.setVoteType(voteInfoDO.getVoteType());

        VoteConfigDO configDO = voteConfigMapper.select(updateReqVO.getVoteId().intValue(),updateReqVO.getDeptId(),updateReqVO.getId());
        if(configDO!=null){
            exception(CONFIG_VOTE_DEPT_EXISTS);
        }


        // 更新
        VoteConfigDO updateObj = VoteConfigConvert.INSTANCE.convert(updateReqVO);
        voteConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfig(Long id) {
        // 校验存在
        this.validateConfigExists(id);
        // 删除
        voteConfigMapper.deleteById(id);
    }

    private void validateConfigExists(Long id) {
        if (voteConfigMapper.selectById(id) == null) {
            throw exception(CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public VoteConfigDO getConfig(Long id) {
        return voteConfigMapper.selectById(id);
    }

    @Override
    public List<VoteConfigDO> getConfigList(Collection<Long> ids) {
        return voteConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VoteConfigDO> getConfigPage(VoteConfigPageReqVO pageReqVO) {
        return voteConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VoteConfigDO> getConfigList(VoteConfigExportReqVO exportReqVO) {
        return voteConfigMapper.selectList(exportReqVO);
    }


    private VoteInfoDO existVote(Long voteId){
        VoteInfoDO voteInfoDO = voteInfoMapper.selectById(voteId);
        if(voteInfoDO==null){
            throw exception(INFO_NOT_EXISTS);
        }
        return voteInfoDO;
    }

    @Override
    public VoteConfigDO selectByVoteId(Integer voteId){
        return voteConfigMapper.selectByVoteId(voteId);
    }
}
